﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Dtd.Schema;
using Engine.Sgml.Matching;
using System.Diagnostics;

namespace Engine.Sgml.Merging {
    internal sealed class OnlyOneCaptureAllocator : ICaptureAllocator {

        public void Insert(Match match, ref Capture capture, ref int index) {
            capture.Entity.Move(match, match.Ancestor, GetNextIndex(match));
            match.Captures.Add(capture);
            Debug.Assert(MatchTestUtility.Verify(match));
            }

        public void Replace(Match match, ref Capture capture, int index) {
            throw new InvalidOperationException();
            }

        public bool Add(Match match, ref Capture capture) {
            return false;
            }

        private static int GetNextIndex(Match match) {
            if (match.Captures.Count > 0 && match.Captures.Last().Entity.IsIllegalElement)
                return match.Captures.Last().EndIndex + 1;
            return match.NextIndex;
            }
        }
    }
